package dingtalk

import (
	"fmt"
	"unsafe"

	"gitee.com/lorock/go-dingtalk-sdk/dingtalkhttplib"

	jsoniter "github.com/json-iterator/go"
	log "github.com/sirupsen/logrus"
)

// UserCreate 创建用户
func (c *Client) UserCreate(user *CreateUser) (*CreateUserRsp, error) {
	rsp := new(CreateUserRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	jsonParms, err := json.Marshal(user)
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", UserCreate, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Post(url).Body(jsonParms).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserUpdate 更新用户
func (c *Client) UserUpdate(user *CreateUser) (*CreateUserRsp, error) {
	rsp := new(CreateUserRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	jsonParms, err := json.Marshal(user)
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", UserUpdate, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Post(url).Body(jsonParms).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserDelete 删除用户
func (c *Client) UserDelete(userid string) (*CreateUserRsp, error) {
	rsp := new(CreateUserRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&userid=%s", UserUpdate, accessToken, userid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGet 获取用户详情
func (c *Client) UserGet(userid string) (*UsertGetRsp, error) {
	rsp := new(UsertGetRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&userid=%s", UserGet, accessToken, userid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGetDeptMember 获取部门用户userid列表
func (c *Client) UserGetDeptMember(userid string) (*UserGetDeptMemberRsp, error) {
	rsp := new(UserGetDeptMemberRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", UserGetDeptMember, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserSimpleList 获取部门用户
func (c *Client) UserSimpleList(departmentid int) (*UserSimpleListRsp, error) {
	rsp := new(UserSimpleListRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&department_id=%d", UserSimpleList, accessToken, departmentid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserListByPage 获取部门用户详情
func (c *Client) UserListByPage(departmentid int) (*UserListByPageRsp, error) {
	rsp := new(UserListByPageRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&department_id=%d", UserListByPage, accessToken, departmentid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGetAdmin 获取管理员列表
func (c *Client) UserGetAdmin() (*UserGetAdminRsp, error) {
	rsp := new(UserGetAdminRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", UserGetAdmin, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGetAdminScope 获取管理员通讯录权限范围
func (c *Client) UserGetAdminScope() (*UserGetAdminScopeRsp, error) {
	rsp := new(UserGetAdminScopeRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", UserGetAdminScope, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGetUseridByUnionid 根据unionid获取userid
func (c *Client) UserGetUseridByUnionid(unionid string) (*UserGetUseridByUnionidRsp, error) {
	rsp := new(UserGetUseridByUnionidRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&unionid=%s", UserGetUseridByUnionid, accessToken, unionid)
	if c.Debug {
		log.Println("url:", url)
	}

	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGetByMobile 根据手机号获取userid
func (c *Client) UserGetByMobile(mobile string) (*UserGetByMobileRsp, error) {
	rsp := new(UserGetByMobileRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&mobile=%s", UserGetByMobile, accessToken, mobile)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGetOrgUserCount 获取企业员工人数
func (c *Client) UserGetOrgUserCount() (*UserGetOrgUserCountRsp, error) {
	rsp := new(UserGetOrgUserCountRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", UserGetOrgUserCount, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// UserGetInactive 未登录钉钉的员工列表
func (c *Client) UserGetInactive() (*UserGetInactiveRsp, error) {
	rsp := new(UserGetInactiveRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", UserGetInactive, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&body))
	log.Println(*str)
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}
